package com.jt.auth.config;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//        初始化密码加密对象
        @Bean
        public BCryptPasswordEncoder passwordEncoder(){
            return new BCryptPasswordEncoder();
        }
        //配置认证管理器(此对象主要负责对客户端输入的用户信息进行认证),在其他配置类中会用到这个对象
        @Bean
        public AuthenticationManager authenticationManagerBean()
                throws Exception {
            return super.authenticationManagerBean();
        }
//        在这个方法中定义登录规则
    /*
    1.对所有请求放行(当前工程只做认证)
    2.登录成功信息的返回
    3.登录失败信息的返回
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //关闭跨域攻击
        http.csrf().disable();
        //放行所有请求
        http.authorizeRequests().anyRequest().permitAll();
        //登录成功与失败的处理
        http.formLogin()
                .successHandler(authenticationSuccessHandler())
                .failureHandler(authenticationFailureHandler());
    }
    @Bean
    public AuthenticationSuccessHandler authenticationSuccessHandler(){
        return (httpServletRequest,httpServletResponse,authentication)->{
            HashMap<String, Object> map = new HashMap<>();
            map.put("state",101);
            map.put("message","login OK");
            //Gson--toJson(gson)
            //fastjson--JSON(spring-cloud-starter-alibaba-sentinel)
            //jackson--writeValueAsString(spring-boot-starter-web)
            //String s = new ObjectMapper().writeValueAsString(map);
            //将json字符串写到客户端
//            PrintWriter writer = httpServletResponse.getWriter();
//            writer.println(s);
//            writer.flush();
            writeValueAsString(httpServletResponse,map);
        };
    }
    @Bean
    public AuthenticationFailureHandler authenticationFailureHandler(){
        return ( httpServletRequest, httpServletResponse,e)->{
            HashMap<String, Object> map = new HashMap<>();
            map.put("state",101);
            map.put("message","login OK");
            writeValueAsString(httpServletResponse,map);
            };
        }
        public void writeValueAsString(HttpServletResponse httpServletResponse,Object o) throws IOException {
            String s = new ObjectMapper().writeValueAsString(o);
            //将json字符串写到客户端
            PrintWriter writer = httpServletResponse.getWriter();
            writer.println(s);
            writer.flush();
        }
    }

